<!--_meta 作为公共模版分离出去-->
{include file="public/meta"}
<!--/meta 作为公共模版分离出去-->
<title>新增领队</title>
<link href="__PUB__/webuploader/0.1.5/webuploader.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="__PUBLIC__/css/ly-upload.css">
</head>
<body>
<div class="page-container">
	<form class="form form-horizontal" id="form-data-add" enctype="multipart/form-data" action="{:url('captain/captainSel')}" method="post">
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>领队名称：</label>
			<div class="formControls col-xs-5 col-sm-4">
                 <input type="text" class="input-text" id="name" value="{$captain_info.name}"  name="name" autocomplete="off">
                 <input type="text" name="id" id="id" value="{$captain_info.id}" hidden="hidden"/>
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>队长头像：</label>
			<div class="formControls col-xs-8 col-sm-9">
				 <div class="formControls col-xs-8 col-sm-9">
                <label class="upload_lable" for="file_img"><img id="captainPhoto" src="{$captain_info.captainPhoto}"/></label>
                <input type="file" name="img" id="file_img" value="" hidden="hidden"/>
            </div>
			</div>
		</div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>背景图：</label>
            <div class="formControls col-xs-8 col-sm-9">
                 <div class="formControls col-xs-8 col-sm-9">
                <label class="upload_lable" for="file_img1"><img id="img1" src="{$captain_info.img1}"/></label>
                <input type="file" name="img1" id="file_img1" value="" hidden="hidden"/>
                   <label class="upload_lable" for="file_img2"><img id="img2" src="{$captain_info.img2}"/></label>
                <input type="file" name="img2" id="file_img2" value="" hidden="hidden"/>
                 <label class="upload_lable" for="file_img3"><img id="img3" src="{$captain_info.img3}"/></label>
                <input type="file" name="img3" id="file_img3" value="" hidden="hidden"/>
                 <label class="upload_lable" for="file_img4"><img id="img4" src="{$captain_info.img4}"/></label>
                <input type="file" name="img4" id="file_img4" value="" hidden="hidden"/>
                 <label class="upload_lable" for="file_img5"><img id="img5" src="{$captain_info.img5}"/></label>
                  <input type="file" name="img5" id="file_img5" value="" hidden="hidden"/>

            </div>
            </div>
        </div>
		<div class="row cl">
			<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                <button onClick="layer_close();" class="btn btn-default radius" type="button">&nbsp;&nbsp;返回列表&nbsp;&nbsp;</button>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <button id="bt_sub" class="btn btn-primary radius" type="button">&nbsp;&nbsp;编辑&nbsp;&nbsp;</button>
            </div>
		</div>
	</form>
</div>
</div>

<!--_footer 作为公共模版分离出去-->
{include file="public/footer"}
<!--/_footer /作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="__PUB__/webuploader/0.1.5/webuploader.min.js"></script> 
<script type="text/javascript">
 function toBase64(file, fun) {
        //参数file为input获取到的file数组第一项，fun向外传递转换成功的base64编码
        var reader = new FileReader();
        if (file) {
            reader.readAsDataURL(file);
            reader.onload = function () {
                fun(this.result);
            }
        }
    }
 $(function(){
        $('#file_img').change(function () {
            var file_img=$(this);
            var img = $('#captainPhoto');
            if (file_img[0].files[0].type.indexOf("image") == -1) {
                file_img.val("");
                img.attr('src',"__PUBLIC__/ic_ph.png");
                layer.msg('请选择图片类型的文件!', {icon: 7, time: 1000});
            } else {
                var files = file_img[0].files[0];
                toBase64(files, function (res) {
                    img.attr('src',res);
                })
            }
        });
        $('#file_img1').change(function () {
            var file_img1=$(this);
            var img1 = $('#img1');
            if (file_img1[0].files[0].type.indexOf("image") == -1) {
                file_img1.val("");
                img1.attr('src',"__PUBLIC__/ic_ph.png");
                layer.msg('请选择图片类型的文件!', {icon: 7, time: 1000});
            } else {
                var files = file_img1[0].files[0];
                toBase64(files, function (res) {
                    img1.attr('src',res);
                })
            }
        });
        $('#file_img2').change(function () {
            var file_img2=$(this);
            var img2 = $('#img2');
            if (file_img2[0].files[0].type.indexOf("image") == -1) {
                file_img2.val("");
                img1.attr('src',"__PUBLIC__/ic_ph.png");
                layer.msg('请选择图片类型的文件!', {icon: 7, time: 1000});
            } else {
                var files = file_img2[0].files[0];
                toBase64(files, function (res) {
                    img2.attr('src',res);
                })
            }
        });
        $('#file_img3').change(function () {
            var file_img3=$(this);
            var img3 = $('#img3');
            if (file_img3[0].files[0].type.indexOf("image") == -1) {
                file_img3.val("");
                img1.attr('src',"__PUBLIC__/ic_ph.png");
                layer.msg('请选择图片类型的文件!', {icon: 7, time: 1000});
            } else {
                var files = file_img3[0].files[0];
                toBase64(files, function (res) {
                    img3.attr('src',res);
                })
            }
        });
        $('#file_img4').change(function () {
            var file_img4=$(this);
            var img4 = $('#img4');
            if (file_img4[0].files[0].type.indexOf("image") == -1) {
                file_img4.val("");
                img1.attr('src',"__PUBLIC__/ic_ph.png");
                layer.msg('请选择图片类型的文件!', {icon: 7, time: 1000});
            } else {
                var files = file_img4[0].files[0];
                toBase64(files, function (res) {
                    img4.attr('src',res);
                })
            }
        });
        $('#file_img5').change(function () {
            var file_img5=$(this);
            var img5 = $('#img5');
            if (file_img5[0].files[0].type.indexOf("image") == -1) {
                file_img5.val("");
                img5.attr('src',"__PUBLIC__/ic_ph.png");
                layer.msg('请选择图片类型的文件!', {icon: 7, time: 1000});
            } else {
                var files = file_img5[0].files[0];
                toBase64(files, function (res) {
                    img5.attr('src',res);
                })
            }
        });
          $('#bt_sub').click(function () {
            var bt_sub = $(this);
            
            var name=$('#name').val();
            if(file_img5=='' || file_img5==undefined){
                layer.msg('请填写领队名称', {icon: 7, time: 1000});
                return false;
            }
            bt_sub.attr('disabled', 'disabled');
            var formData = new FormData();
            formData.append('img',$("#file_img")[0].files[0]);
            formData.append('img1',$("#file_img1")[0].files[0]);
            formData.append('img2',$("#file_img2")[0].files[0]);
            formData.append('img3',$("#file_img3")[0].files[0]);
            formData.append('img4',$("#file_img4")[0].files[0]);
            formData.append('img5',$("#file_img5")[0].files[0]);
           
            $.each($("#form-data-add").serializeArray(), function (i, field) {
                formData.append(field.name, field.value);
                formData.append(field.name, field.value);
                formData.append(field.name, field.value);
                formData.append(field.name, field.value);
                formData.append(field.name, field.value);
                formData.append(field.name, field.value);
            });
            $.ajax({
                url: $('#form-data-add').attr('action'),
                type: 'POST',
                data: formData,
                processData: false,// 告诉jQuery不要去处理发送的数据
                contentType: false,// 告诉jQuery不要去设置Content-Type请求头
                beforeSend: function () {
                    layer.msg('正在上传，请不要刷新或关闭页面!', {icon: 7, time: 1000000});
                },
                success: function (res) {
                     if (res.state == 1) {
                         window.parent.layer.msg(res.msg, {icon: 1, time: 1000});
                        setTimeout(function () {
                             window.parent.location.reload();
                         }, 1200);
                      } else {
                         window.parent.layer.msg(res.msg, {icon: 1, time: 1000});
                         setTimeout(function () {
                             window.parent.location.reload();
                         }, 3600);
                     }
                },
                error: function (res) {
                    layer.msg('网络错误，请关闭重试!', {icon: 2, time: 1000});
                    bt_sub.removeAttr('disabled');
                }
            });
        });
   });     

</script>
</body>
</html>